package com.scalar.forecast.forecastEntry.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastEntry.forms.ForecastEntryForm;
import com.scalar.forecast.forecastEntry.TableMetaData;
import com.scalar.forecast.forecastSetup.ForecastTimeFrame;
import com.scalar.forecast.forecastSetup.dao.ForecastSetupDAO;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.chiptrack.users.User;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.LinkedList;

/**
 * User: Sujan Kumar
 * Date: Feb 18, 2005
 * Time: 12:24:33 PM
 */
public class ForecastEntrySalesReloadTableColumnsCommand extends ActionCommand
{
    public ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
    {
        HttpSession session = request.getSession();
        HashMap errors = new HashMap ();
        ForecastEntryForm forecastEntryForm = ( ForecastEntryForm )form;
        
        User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY );
        String userId = pUser.getUserId();
        TableMetaData tableMetaData = ForecastEntrySalesNewCommand.getTableMetaData( forecastEntryForm, userId );
        ForecastEntrySalesNewCommand.loadUserDefinedFieldValuesInRequest ( tableMetaData.getTableColumnList(), request );
        forecastEntryForm.setTableMetaData( tableMetaData );
        try
        {
            ForecastTimeFrame timeframe = ForecastSetupDAO.getForecastTimeFrame(null);
            if ( "Month".equalsIgnoreCase( timeframe.getTfName() ) )
            {
                request.setAttribute ( "showNext", "Months" );
            }
            else if ( "Quarter".equalsIgnoreCase( timeframe.getTfName() ) )
            {
                request.setAttribute ( "showNext", "Quarters" );
            }
            else if ( "Year".equalsIgnoreCase( timeframe.getTfName() ) )
            {
                request.setAttribute ( "showNext", "Years" );
            }
        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_FORECAST_TIMEFRAME );
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception while getting the timeframe-----");
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        try
        {
            LinkedList backlogInfoList = ForecastEntrySalesNewCommand.getBackLogInfoList( null, forecastEntryForm );
            request.setAttribute( "backlogInfoList", backlogInfoList );
        }
        catch ( ScalarException e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_BACKLOG_INFO );
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "----------exception while getting backlog information-----");
            e.printStackTrace();
            return mapping.findForward ( "searchPage" );
        }
        ActionForward actionForward = mapping.findForward ( "forecastEntrySales");
        return actionForward;
    }
}
